<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="举报类型" prop="type	">
        <el-select v-model="queryParams.type">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="举报时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="handleDateChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="mb8" style="display: flex;align-items: center;justify-content: space-between">
      <div class="tag-group">
        <el-tag
          v-for="(item, index) in tagItems"
          :key="item.label"
          :effect="tagIndex === index ? 'dark' : 'plain'"
          style="margin-right: 10px"
          @click="handleClick(index)">
          {{ item.label }}
        </el-tag>
      </div>
      <div>
        <el-button
          type="default"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </div>
    </div>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" width="80" align="center" sortable />
      <el-table-column label="举报类型" align="center" prop="typeDesc" />
      <el-table-column label="举报原因" align="center" prop="reason" />
      <el-table-column label="举报图片" align="center" prop="pic">
        <template slot-scope="scope" v-if="scope.row.pic">
          <div style="padding: 10px 0;">
            <el-badge :value="scope.row.pic.length" class="item">
              <el-image :src="scope.row.pic.length ? scope.row.pic[0] : ''" :preview-src-list="scope.row.pic" style="width: 50px;height: 50px;" fit="cover"></el-image>
            </el-badge>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属模板" align="center" prop="templateContent">
        <template slot-scope="scope" v-if="scope.row.templateType === 1 ? scope.row.templateCover : scope.row.templateContent">
          <el-image :src="scope.row.templateType === 1 ? scope.row.templateCover : scope.row.templateContent" @click="handleView(scope.row)" style="width: 50px;height: 50px;" fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="所属标题" align="center" prop="templateTitle" />
      <el-table-column label="所属分类" align="center" prop="categoryName" />
      <el-table-column label="举报时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status === 1 ? '未处理' : scope.row.status === 2 ? '已处理' : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 1"
            @click="handleRefuse(scope.row, 3)"
          >忽略</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status !== 2"
            @click="handleRefuse(scope.row, 2)"
          >下架</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog title="内容查看" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-image :src="content.templateContent" v-if="content.templateType === 2" style="height: 400px" fit="cover"></el-image>
      <video :src="content.templateContent" v-else-if="content.templateType === 1" style="width: 100%;height: 400px;" controls></video>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getReportList, getReportStatus, checkReportStatus, removeReportInfo } from "@/api/shuiyin/shuiyin";

  export default {
    name: "ReportManage",
    data() {
      return {
        // 举报类型
        typeOptions: [
          {
            label: '违法违禁',
            value: 1
          },
          {
            label: '色情暴力',
            value: 2
          },
          {
            label: '赌博诈骗',
            value: 3
          },
          {
            label: '社政谣言',
            value: 4
          },
          {
            label: '其他',
            value: 5
          }
        ],
        // 处理状态
        statusOptions: [
          {
            label: '未处理',
            value: 1
          },
          {
            label: '已处理',
            value: 2
          }
        ],
        // 位置数据
        tagItems: [
          {
            value: '',
            label: '全部'
          }, {
            value: '2',
            label: '已处理(0)'
          }, {
            value: '1',
            label: '未处理(0)'
          }
        ],
        // 位置下标
        tagIndex: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 租户管理表格数据
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          type: '',
          reportBeginDate: null,
          reportEndDate	: null,
          status: null,
          pageNum: 1,
          pageSize: 10,
        },
        content: {}
      };
    },
    created() {
      this.getList();
      this.getSetInfo();
    },
    methods: {
      /** 查询租户管理列表 */
      getList() {
        this.loading = true;
        getReportList(this.queryParams).then(response => {
          const array = response.rows || [];
          // array.forEach(element => {
          //   element.pic = element.pic ? element.pic.split(',') : [];
          // })
          this.list = array;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 日期时间选择组件确定事件
      handleDateChange(event) {
        this.queryParams.reportBeginDate = event[0];
        this.queryParams.reportEndDate = event[1];
      },
      getSetInfo() {
        getReportStatus().then(response => {
          if (response.data) {
            this.tagItems[1].label = `已处理(${response.data.haveResolveNum})`
            this.tagItems[2].label = `未处理(${response.data.preResolveNum})`
          }
        })
      },
      // tag标签点击事件
      handleClick(index) {
        this.tagIndex = index;
        this.queryParams.status = this.tagItems[index].value;
        this.queryParams.pageNum = 1;
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.reportBeginDate = null;
        this.queryParams.reportEndDate = null;
        this.dateRange = [];
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 忽略操作按钮 */
      handleRefuse(row, status) {
        this.$modal.confirm(`是否确认${status === 3 ? '忽略' : '下架'}所选数据项？`).then(function() {
          return checkReportStatus({
            id: row.id,
            checkStatus: status
          });
        }).then(() => {
          this.getList();
          this.getSetInfo();
          this.$modal.msgSuccess("忽略成功");
        }).catch(() => {});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const memberIds = row.id || this.ids;
        this.$modal.confirm('是否确认删除所选数据项？').then(function() {
          return removeReportInfo(memberIds);
        }).then(() => {
          this.getList();
          this.getSetInfo();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 内容查看操作 */
      handleView(row) {
        this.content = JSON.parse(JSON.stringify(row));
        this.open = true;
      }
    }
  };
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
</style>
